package com.example.demo.controllers;

import com.example.demo.form.ChanceLikeForm;
import com.example.demo.form.GetWordsFrom;
import com.example.demo.form.LearnAndReviewForm;
import com.example.demo.pojo.Word;
import com.example.demo.service.IWordsService;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author luolinila
 */
@RestController
public class WordsController {

    @Autowired
    private IWordsService wordsService;

    @GetMapping(value = "/word/seek")
    public Result seek(@RequestParam("SPELL") String spell) {
        Word word = wordsService.getWordsBySpell(spell);
        if(word != null) {
            return Result.success(word);
        } else {
            return new Result(10007, "单词不存在");
        }
    }

    @PostMapping(value = "/word/learn")
    public Result learn(@RequestBody GetWordsFrom getWordsFrom) {

        System.out.println(getWordsFrom.getUserId() + " " + getWordsFrom.getBookName() + " " + getWordsFrom.getCount());

        List<Word> list = wordsService.getWords(getWordsFrom.getBookName(), getWordsFrom.getCount()+1, wordsService.getLearn(getWordsFrom.getUserId(), getWordsFrom.getBookName()));
        changeLearn(new LearnAndReviewForm(getWordsFrom.getUserId(), getWordsFrom.getBookName(), list.get(list.size()-1).getId()));
        return Result.success(list.subList(0, list.size()-1));
    }

    @PostMapping(value = "/word/review")
    public Result review(@RequestBody GetWordsFrom getWordsFrom) {

        System.out.println(getWordsFrom.getUserId() + " " + getWordsFrom.getBookName() + " " + getWordsFrom.getCount());

        List<Word> list = wordsService.getWords(getWordsFrom.getBookName(), getWordsFrom.getCount()+1, wordsService.getReview(getWordsFrom.getUserId(), getWordsFrom.getBookName()));
        if(list.get(getWordsFrom.getCount()-1).getId() >= wordsService.getLearn(getWordsFrom.getUserId(), getWordsFrom.getBookName())) {
            int last = list.indexOf(wordsService.getById(wordsService.getLearn(getWordsFrom.getUserId(), getWordsFrom.getBookName())));
            if(list.get(last).getId() <= list.get(0).getId()) {
                return Result.success(null);
            }
            changeReview(new LearnAndReviewForm(getWordsFrom.getUserId(), getWordsFrom.getBookName(), wordsService.getLearn(getWordsFrom.getUserId(), getWordsFrom.getBookName())));
            return Result.success(list.subList(0, last));
        }
        changeReview(new LearnAndReviewForm(getWordsFrom.getUserId(), getWordsFrom.getBookName(), list.get(list.size()-1).getId()));
        return Result.success(list.subList(0, list.size()-1));
    }

    @GetMapping(value = "/word/like/user/")
    public Result userLike(@RequestParam("USER_ID") Integer userId) {
        return Result.success(wordsService.getUserLike(userId));
    }

    @PostMapping(value = "/word/like/word")
    public Result changeLike(@RequestBody ChanceLikeForm chanceLikeForm) {
        if(chanceLikeForm.getStatus()) {
            wordsService.addUserLike(chanceLikeForm.getUserId(), chanceLikeForm.getWordId());
        }else {
            wordsService.deleteUserLike(chanceLikeForm.getUserId(), chanceLikeForm.getWordId());
        }
        return Result.success(null);
    }

    @GetMapping(value = "/word/like/check")
    public Result checkUserLike(@RequestBody ChanceLikeForm chanceLikeForm) {
        return Result.success(wordsService.isUserLike(chanceLikeForm.getUserId(), chanceLikeForm.getWordId()));
    }

    @PostMapping(value = "/word/change/learn")
    public Result changeLearn(@RequestBody LearnAndReviewForm learnAndReviewForm) {
        wordsService. updateLearn(learnAndReviewForm.getUserId(), learnAndReviewForm.getBookName(), learnAndReviewForm.getWordId());
        if(wordsService.getLearn(learnAndReviewForm.getUserId(), learnAndReviewForm.getBookName()) < wordsService.getReview(learnAndReviewForm.getUserId(), learnAndReviewForm.getBookName())) {
            changeReview(new LearnAndReviewForm(learnAndReviewForm.getUserId(), learnAndReviewForm.getBookName(), learnAndReviewForm.getWordId()));
        }
        return Result.success(null);
    }

    @PostMapping(value = "/word/change/review")
    public Result changeReview(@RequestBody LearnAndReviewForm learnAndReviewForm) {
        if (learnAndReviewForm.getWordId() > wordsService.getLearn(learnAndReviewForm.getUserId(), learnAndReviewForm.getBookName())) { return new Result(10008,"review大于learn"); }
        wordsService.updateReview(learnAndReviewForm.getUserId(), learnAndReviewForm.getBookName(), learnAndReviewForm.getWordId());
        return Result.success(null);
    }

    @GetMapping(value = "/word/collection/seek")
    public Result collectionSeek(@RequestParam("WORD") Integer wordId) {
        wordsService.updateWordCollection(wordId);
        return Result.success(wordsService.getWordCollection(wordId));
    }

    @GetMapping(value = "word/book")
    public Result book(@RequestParam("BOOK_NAME") String bookName) {
        return Result.success(wordsService.getWordsByBook(bookName));
    }
}
